/**
 *
 * User: Zhikang.Peng
 * Date: 2018/06/19 17:14
 */
package heap

// 创建原始类型的数组
func (self *Class) NewArray(count uint) *Object {
	if !self.IsArray() {
		panic("Not array class: " + self.name)
	}
	switch self.name {
	case "[Z": // boolean
		return &Object{self, make([]int8, count), nil}
	case "[B": // byte
		return &Object{self, make([]int8, count), nil}
	case "[C": // char
		return &Object{self, make([]uint16, count), nil}
	case "[S": // short
		return &Object{self, make([]int16, count), nil}
	case "[I": // int
		return &Object{self, make([]int32, count), nil}
	case "[J": // long
		return &Object{self, make([]int64, count), nil}
	case "[F": // float
		return &Object{self, make([]float32, count), nil}
	case "[D": // double
		return &Object{self, make([]float64, count), nil}
	default: // reference
		return &Object{self, make([]*Object, count), nil}
	}
}

// 加载对象类型数组类
func (self *Class) ArrayClass() *Class {
	arrayClassName := getArrayClassName(self.name)
	return self.classLoader.LoaderClass(arrayClassName)
}

// 返回数组类的元素类型
func (self *Class) ComponentClass() *Class {
	componentClassName := getComponentClassName(self.name)
	return self.classLoader.LoaderClass(componentClassName)
}

func (self *Class) IsArray() bool {
	return self.name[0] == '['
}

// 是否是多维数组
func (self *Class) IsMultiaArray() bool {
	if len(self.name) <= 1 {
		return false
	}
	return self.name[0] == '[' && self.name[1] == '['
}

func NewByteArray(loader *ClassLoader, bytes []int8) *Object {
	return &Object{loader.LoaderClass("[B"), bytes, nil}
}
